<?php

/**
 * ImageFile class
 *
 * @author Julot <andy.julot@gmail.com>
 * @since Tuesday, June 21, 2011. 03:44 PM
 */
class ImageFile extends File {

	protected $_image = null;

	/**
	 * Resample image to desired size.
	 * @param integer desired width.
	 * @param integer desired height.
	 * @param boolean width and height ratio.
	 * @return image on success, null otherwise.
	 */
	public function resample($width = 0, $height = 0, $crop = false) {
		if (!$old = $this->createImage())
			return null;

		if (!list($w, $h) = getimagesize($this->_path))
			return null;

		if ($w < $width && $h < $height)
			return $old;

		if (!$width)
			$width = $w;
		if (!$height)
			$height = $h;
		if ($crop) {
			$ratio = max($width / $w, $height / $h);
			//$y = ($h - $height / $ratio) / 2;
			$y = 0;
			$h = $height / $ratio;
			$x = ($w - $width / $ratio) / 2;
			$w = $width / $ratio;
		} else {
			$ratio = min($width / $w, $height / $h);
			$width = $w * $ratio;
			$height = $h * $ratio;
			$x = 0;
			$y = 0;
		}
		$new = imagecreatetruecolor($width, $height);
		if ($this->extension == "gif" || $this->extension == "png") {
			$alpha = imagecolorallocatealpha($new, 0, 0, 0, 127);
			imagecolortransparent($new, $alpha);
			imagealphablending($new, false);
			imagesavealpha($new, true);
		}
		$resampled = imagecopyresampled(
			$new, $old, 0, 0, $x, $y, $width, $height, $w, $h
		);
		if ($resampled)
			return $new;
		return null;
	}

	/**
	 * Create image from file.
	 * @return image on success, null otherwise.
	 */
	public function createImage() {
		if ($this->_image !== null)
			return $this->_image;
		if (!$info = getimagesize($this->_path))
			return null;
		$type = $info[2];
		if ($type == 1): // GIF
			$this->_image = @imagecreatefromgif($this->_path);
		elseif ($type == 2): // JPG
			$this->_image = @imagecreatefromjpeg($this->_path);
		elseif ($type == 3): // PNG
			$this->_image = @imagecreatefrompng($this->_path);
		elseif ($type == 4): // SWF
			return null;
		elseif ($type == 5): // PSD
			return null;
		elseif ($type == 6): // BMP
			return null;
		elseif ($type == 7): // TIFF(intel byte order)
			return null;
		elseif ($type == 8): // TIFF(motorola byte order)
			return null;
		elseif ($type == 9): // JPC
			return null;
		elseif ($type == 10): // JP2
			return null;
		elseif ($type == 11): // JPX
			return null;
		elseif ($type == 12): // JB2
			return null;
		elseif ($type == 13): // SWC
			return null;
		elseif ($type == 14): // IFF
			return null;
		elseif ($type == 15): // WBMP
			$this->_image = @imagecreatefromwbmp($this->_path);
		elseif ($type == 16): // XBM
			$this->_image = @imagecreatefromxbm($this->_path);
		endif;
		return $this->_image;
	}

	/**
	 * Save image as png.
	 * @param string image file destination actual file path on disc.
	 * @param integer desired destination image width.
	 * @param integer desired destination image height.
	 * @param boolean width and height ratio.
	 * @return raw image stream if path is null or create image if path
	 *         specified.
	 */
	public function saveAsPng($path = null, $width = null, $height = null,
		$crop = false) {
		$resampled = $this->resample($width, $height, $crop);
		return imagepng($resampled, $path);
	}

	/**
	 * Save image as jpg.
	 * @param string image file destination actual file path on disc.
	 * @param integer desired destination image width.
	 * @param integer desired destination image height.
	 * @param boolean width and height ratio.
	 * @param integer desired quality. 0 worst, 100 best.
	 * @return raw image stream if destination is null.
	 */
	public function saveAsJpeg($path = null, $width = 0, $height = 0,
		$crop = false, $quality = 75) {
		$resampled = $this->resample($width, $height, $crop);
		return imagejpeg($resampled, $path, $quality);
	}

}
